package com.dingpeikeji.quickdeer.modules.wx.orders.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.core.modules.orders.service.TOrdersService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.modules.realsche.service.FRealscheService;
import com.dingpeikeji.quickdeer.modules.sms.entity.Sms;
import com.dingpeikeji.quickdeer.modules.sms.service.SmsApi;
import com.dingpeikeji.quickdeer.modules.wx.common.web.OAuth;

/**
 * 微信端班次车订票
 */
@Controller
@RequestMapping("${wxPath}/orders")
public class WxOrdersController extends BaseController{
	
	@Autowired
	private FRealscheService fRealscheService;
	@Autowired
	private TOrdersService tOrdersService;
	
	@RequestMapping("orderList")
	public String orderHead(Model model) {
		model.addAttribute("navStyle", "orderListPage");
		return "wx/modules/orders/orderList";
	}
	/*@RequestMapping(value="orderList",method=RequestMethod.POST)
	@ResponseBody
	public Page<TOrders> listJson(TOrders orders, Model model, HttpServletRequest request, HttpServletResponse response){
		User user = (User)request.getSession().getAttribute(OAuth.SESSION_PUBUSER);
		orders.setOrderuserid(user.getId());
		return fRealscheService.findOrderPage(new Page<TOrders>(request, response), orders);
	}*/
	
	@RequestMapping(value="comfirmPay",method=RequestMethod.POST)
	@ResponseBody
	public void comfirmPay(TOrders orders,String transaction_id, HttpServletRequest request){
		User user = (User)request.getSession().getAttribute(OAuth.SESSION_PUBUSER);
		fRealscheService.proConfirmPay(Integer.parseInt(orders.getOrderid()), Double.parseDouble(orders.getDueamount()), transaction_id, user.getWechatId(), TOrders.PAY_WAY_WXPAY);
		
		// 发短信给用户
		Sms sendSms = tOrdersService.composeSms(orders.getOrderid());
		if (sendSms != null){
			try {
				SmsApi.sendSms(sendSms);
			} catch (Exception e) {
			}
		}
	}
	@RequestMapping("orderDetail")
	public String orderDetailPage(Model model,HttpServletRequest request) {
		model.addAttribute("orderid",request.getParameter("orderid"));
		return "wx/modules/orders/orderDetail";
	}
	@RequestMapping("backMoneyPage")
	public String backMoney(Model model,HttpServletRequest request) {
		String orderid = request.getParameter("orderid");
		model.addAttribute("orderid", orderid);
		return "wx/modules/orders/backMoney";
	}
}
